package com.bingxue.edu.management.auth;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import com.bingxue.edu.common.enums.EmployeeTypeEnum;
import com.bingxue.edu.framework.common.constant.CacheConstants;
import com.bingxue.edu.framework.common.util.SpringContextHolder;
import com.bingxue.edu.management.auth.model.AuthEmployee;
import com.bingxue.edu.management.auth.service.EmployeeAuthService;
import com.bingxue.edu.management.system.model.entity.Employee;
import com.bingxue.edu.management.system.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;

/**
 * @author panbo
 * @since 2024/8/24
 */
@Slf4j
public class EmployeeAuthUtil {

    public static final StpLogic STP = StpUtil.stpLogic;

    static {
        configEmployeeStpLogic();
    }

    private static void configEmployeeStpLogic() {
        SaTokenConfig config = new SaTokenConfig();
        config.setTokenName("t-token");
        config.setTokenStyle("simple-uuid");
        config.setIsReadBody(false);
        config.setIsReadCookie(false);
        STP.setConfig(config);
    }

    /**
     * 获取当前登录用户id
     */
    public static String getCurrentUserId() {
        Object loginId = STP.getLoginIdDefaultNull();
        return loginId == null ? null : loginId.toString();
    }

    /**
     * 登录并缓存用户信息
     * @param authEmployee 认证用户信息
     */
    public static String login(AuthEmployee authEmployee) {
        STP.login(authEmployee.getUserInfo().getId());
        String tokenValue = STP.getTokenValue();
        authEmployee.setToken(tokenValue);
        SaHolder.getStorage().set(CacheConstants.LOGIN_USER_KEY, authEmployee);
        STP.getTokenSession().set(CacheConstants.LOGIN_USER_KEY, authEmployee);
        log.info("员工[{}]登录成功", authEmployee.getUserInfo().getId());
        return tokenValue;
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 登录用户信息
     * @throws NotLoginException 未登录异常
     */
    public static AuthEmployee getAuthUser() throws NotLoginException {
        STP.checkLogin();
        AuthEmployee authEmployee = (AuthEmployee)SaHolder.getStorage().get(CacheConstants.LOGIN_USER_KEY);
        if (null != authEmployee) {
            return authEmployee;
        }
        SaSession tokenSession = STP.getTokenSession();
        authEmployee = (AuthEmployee)tokenSession.get(CacheConstants.LOGIN_USER_KEY);
        if (null == authEmployee) {
            log.warn("员工用户信息为空, token: {}, loginId: {}", STP.getTokenValue(), STP.getLoginId());
            EmployeeService employeeService = SpringContextHolder.getBean(EmployeeService.class);
            EmployeeAuthService employeeAuthService = SpringContextHolder.getBean(EmployeeAuthService.class);
            Employee employee = employeeService.getById(STP.getLoginIdAsString());
            authEmployee = employeeAuthService.buildAuthEmployee(employee);
            authEmployee.setToken(STP.getTokenValue());
            STP.getTokenSession().set(CacheConstants.LOGIN_USER_KEY, authEmployee);
        }
        SaHolder.getStorage().set(CacheConstants.LOGIN_USER_KEY, authEmployee);
        return authEmployee;
    }

    /**
     * 根据token获取登录用户信息
     *
     * @return 登录用户信息
     * @throws NotLoginException 未登录异常
     */
    public static AuthEmployee getAuthUser(String token) {
        SaSession tokenSession = STP.getTokenSessionByToken(token, false);
        if (null == tokenSession) {
            return null;
        }
        return (AuthEmployee)tokenSession.get(CacheConstants.LOGIN_USER_KEY);
    }

    /**
     *  判断是否是辅导教师
     */
    public static boolean isAssistTeacher(){
        AuthEmployee employee = getAuthUser();
        return EmployeeTypeEnum.ASSIST_TEACHER.equals(employee.getUserInfo().getType());
    }
}
